package com.algrithom.linktable;

import com.common.model.ListNode;

/**
 * 61. 旋转链表
 */
class Solution2 {
    
    public static void main(String[] args){
        
    }
    
    public static ListNode<Integer> rotateRight(ListNode<Integer> head,int k){
        if (head == null) {
            return null;
        }
        if (head.getNext() == null) {
            return head;
        }
        
        ListNode<Integer> oldTail = head;
        int n;
        for (n = 1; oldTail.getNext() != null; n++) {
            oldTail = oldTail.getNext();
        }
        oldTail.setNext(head);
        
        ListNode<Integer> newTail = head;
        for (int i = 0; i < n - k % n - 1; i++) {
            newTail = newTail.getNext();
        }
        ListNode<Integer> newHead = newTail.getNext();
        
        newTail.setNext(null);
        
        return newHead;
    }
}
